<template>
  <div class="container">
    <el-card class="table-container">
      <div style="margin: 0 10px 20px 10px">
        <el-form :inline="true" :model="searchForm" class="demo-form-inline">
                  <el-form-item
            label="标题"
            prop="parentId"
        >
      <el-col :span="22">
        <el-input :rows="5" v-model="searchForm.parentId" clearable type="text"
                  placeholder="请输入标题"/>
      </el-col>
      </el-form-item>
        <el-form-item
            label="创建人"
            prop="departmentCode"
        label-width="80px">
      <el-col :span="22">
        <el-input :rows="5" v-model="searchForm.departmentCode" clearable type="text"
                  placeholder="请输入创建人"/>
      </el-col>
      </el-form-item>
        <el-form-item
            label="创建时间"
            prop="departmentName"
        label-width="80px">
      <el-col :span="22">
        <el-input :rows="5" v-model="searchForm.departmentName" clearable type="text"
                  placeholder="请输入创建时间"/>
      </el-col>
      </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="handleQuery">查询</el-button>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="handleAdd">新增</el-button>
  </el-form-item>
  </el-form>
  </div>

<div v-for="cardItem in tableData.list" class="box-card">
      <el-card>
        <div slot="header" class="clearfix">
          <span>{{cardItem.title}}</span>
        </div>
              <div class="text item">
                 标题:{{cardItem.parentId}}
              </div>
              <div class="text item">
                 创建人:{{cardItem.departmentCode}}
              </div>
              <div class="text item">
                 创建时间:{{cardItem.departmentName}}
              </div>
              <div class="text item">
                 :{{cardItem.state}}
              </div>
              <div class="text item">
                 :{{cardItem.createPerson}}
              </div>
              <div class="text item">
                 :{{cardItem.createTime}}
              </div>
              <div class="text item">
                 :{{cardItem.name}}
              </div>
      </el-card>

      </div>
  <!-- 分页信息 -->
  <div class="pagination-container">
    <el-pagination background
                   layout="total, prev, pager, next, jumper"
                   @current-change="handlePageChange"
                   @size-change="handleSizeChange"
                   :current-page="pageNum"
                   :total="tableData.total">
    </el-pagination>
  </div>
  </el-card>

  <!-- 新增/编辑对话框 -->
  <el-dialog :title="mode==='add'?'新增':'编辑'"
             :visible.sync="showEditDialog"
             :before-close="handleDialogClose">
    <department-edit :mode="mode"
    ref="editForm"
    :record="currentRecord"
    @after-edit="handleAfterEdit"/>
  </el-dialog>
  </div>
</template>

<script>
import {Message} from 'element-ui'
import DepartmentApi from '../api/department'
import departmentEdit from './departmentEdit'
import ImportExcel from '@/components/ImportExcel'
export default {
  name: 'DepartmentList',
  components: {
    departmentEdit,
    ImportExcel
  },
  data() {
    return {
      /* 搜索请求数据*/
      searchForm: {},
      pageSize: 10,
      pageNum: 1,
      // 表格数据
      tableData: {
        results: []
      },
      mode: 'edit',
      // 是否显示添加/编辑对话框
      showEditDialog: false,
      // 当前处理行
      currentRecord: {},
      queryLoading: false,

    }
  },
  mounted() {
    this.init()
  },
  methods: {
    // 页面初始化
    init() {
      this.handleQuery()
    },
    handleQuery() {
      this.pageNum = 1
      this.query()
    },
    query() {
      return new Promise((resolve, reject) => {
        this.queryLoading = true
        const param = Object.assign({}, this.searchForm)
        DepartmentApi
            .queryByCond(param, this.pageSize, this.pageNum)
            .then(response => {
              const data = response.data
              if (data.code === 200) {
                const data = response.data.data
                this.tableData = data
              } else {
                Message.error(data.message)
                reject()
              }
              this.queryLoading = false
            })
            .catch(error => {
              if (error.message !== '') {
                //Message.error(error.message)
              }
              this.queryLoading = false
              reject(error)
            })
      })
    },
    mockData() {
      setTimeout(() => {
        this.tableData = {total: 3, list: [
          {
            'parentId': '标题-Data-1',
            'departmentCode': '创建人-Data-1',
            'departmentName': '创建时间-Data-1',
            'state': '-Data-1',
            'createPerson': '-Data-1',
            'createTime': '-Data-1',
            'name': '-Data-1',
          },
          {
            'parentId': '标题-Data-2',
            'departmentCode': '创建人-Data-2',
            'departmentName': '创建时间-Data-2',
            'state': '-Data-2',
            'createPerson': '-Data-2',
            'createTime': '-Data-2',
            'name': '-Data-2',
          },
          {
            'parentId': '标题-Data-3',
            'departmentCode': '创建人-Data-3',
            'departmentName': '创建时间-Data-3',
            'state': '-Data-3',
            'createPerson': '-Data-3',
            'createTime': '-Data-3',
            'name': '-Data-3',
          },
        ]}
      }, 1000)
    },
    handleAdd() {
      this.mode = 'add'
      this.currentRecord = {}
      this.showEditDialog = true
    },
    handleDetail(row) {
      this.mode = 'details'
      this.currentRecord = Object.assign({}, row)
      this.showEditDialog = true
    },
    handleEdit(row) {
      this.mode = 'edit'
      this.currentRecord = Object.assign({}, row)
      this.showEditDialog = true
    },

    // 处理翻页事件
    handlePageChange(currentPage) {
      this.pageNum = currentPage
      this.query()
    },
    // 处理页面总条数事件
    handleSizeChange(val) {
      this.pageSize = val;
      this.query()
    },
    // 处理对话框关闭事件
    handleDialogClose(done) {
      this.$refs.editForm.resetFields()
      done()
    },
    handleDel(row) {
      this.$confirm('确定删除吗？', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
          .then(() => {
            const param = Object.assign({}, row)
            DepartmentApi
                .delete(param)
                .then(response => {
                  const data = response.data
                  if (data.code === 200) {
                    this.$message({
                      message: '删除成功',
                      type: 'success'
                    })
                    this.handleQuery()
                  } else {
                    Message.error(data.message)
                  }
                })
                .catch(error => {
                  //console.log(error)
                  if (error.message !== '') {
                    //Message.error(error.message)
                  }
                })
          }).catch(() => {
      })
    },
    // 处理编辑对话框关闭事件
    handleAfterEdit() {
      this.showEditDialog = false
      this.$refs.editForm.resetFields()
      this.query()
    }
  }
}
</script>
<style>
.container {
  margin: 10px;
}

.table-container {
  margin-top: 0px;
}

.pagination-container {
  margin-top: 30px;
  text-align: right;
}
.box-card{
    display:inline-block;
    width: 24%;
    margin: 5px;
}
</style>

